/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaBusServiceRel;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;
import han.bifang.common.utils.CommonUtils;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class BusServiceRelDao {
	
	public static void createBusServiceRef(SoaBusServiceRel s) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			
			DaoManager.getDao().save(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("BusServiceRelDao.createBusServiceRef()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createBusServiceRef() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void createBusServiceRef(List s) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			
			DaoManager.getDao().saveOrUpdateAll(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("BusServiceRelDao.createBusServiceRef(List)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createBusServiceRef(List) error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void modifyBusServiceRef(SoaBusServiceRel s) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaBusServiceRel s where s.compId.serviceId = ?";
			List ret = DaoManager.getDao().find(hql,s.getCompId().getServiceId());
			DaoManager.getDao().deleteAll(ret);
			if(! CommonUtils.isEmpty(s.getCompId().getBusId())){
				DaoManager.getDao().save(s);
			}
			DaoManager.getDao().flush();
			
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("BusServiceRelDao.modifyServiceRef()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyServiceRef() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void modifyRemoteBusServiceRef(List<SoaBusServiceRel> l) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaBusServiceRel s where s.compId.serviceId = ?";
			System.out.println("###@@" + l);
			if(l.size() > 0){
				SoaBusServiceRel s = l.get(0);
				List ret = DaoManager.getDao().find(hql,s.getCompId().getServiceId());
				System.out.println("###" + ret);
				DaoManager.getDao().deleteAll(ret);
				//DaoManager.getDao().flush();
				for(SoaBusServiceRel tt : l){
					DaoManager.getDao().save(tt);
				}
				//DaoManager.getDao().saveOrUpdateAll(l);
				DaoManager.getDao().flush();
			}
			
			
			
			
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("BusServiceRelDao.modifyRemoteBusServiceRef()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyRemoteBusServiceRef() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static List getBusServiceRefByServiceId(String id) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaBusServiceRel s where s.compId.serviceId = ?";
			List ret = DaoManager.getDao().find(hql,id);
			
			tx.commit();
			return ret;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("BusServiceRelDao.getBusServiceRefByServiceId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getBusServiceRefByServiceId() error", e);
		} finally {
			tx.end();
		}

	}
	
	
	
	
	
	public static void removeBusServiceRefByServiceId(String id) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaBusServiceRel s where s.compId.serviceId = ?";
			List ret = DaoManager.getDao().find(hql,id);
			if(ret.size() > 0)
				DaoManager.getDao().deleteAll(ret);
			tx.commit();
			return ;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("BusServiceRelDao.removeBusServiceRefByServiceId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeBusServiceRefByServiceId() error", e);
		} finally {
			tx.end();
		}

	}


}
